/**

 @Name：layuiAdmin（iframe版） 消息中心
 @Author：贤心
 @Site：http://www.layui.com/admin/
 @License：LPPL

 */
layui.define(['admin', 'table', 'util','upload'], function (exports) {
    var $ = layui.$, admin = layui.admin, table = layui.table, element = layui.element, form = layui.form,util = layui.util, upload = layui.upload;
    var addtime = function(d){
        return util.toDateString(d.addtime*1000,'yyyy-MM-dd HH:mm:ss');
    };
    //消息列表
    tableIn = table.render({
        elem: '#list',
        url: '/shengteng/wmsg/getmsg',
        method: 'post',
        where:{openid: OPENID},
        cols: [[
            {field: 'headimgurl',align: 'center', title: '头像', width: 80, templet: '#avatar'}
            , {field: 'nickname', title: '昵称', width: 120,templet: '#nickname'}
            , {field: 'content', title: '内容', width: 300}
            , {field: 'create_time', title: '时间', width: 170,templet: addtime}
            ,{width: 160, align: 'center', toolbar: '#action'}
        ]],
        page:true
    });
    //编辑提交
    form.on('submit(replyMsg)', function (data) {
        admin.req({
            url: '/shengteng/Wmsg/reply/' //实际使用请改成服务端真实接口
            , type: 'post'
            , data: data.field
            , done: function (res) {
                layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                    window.location.reload();
                });
            }
        });
    });
    //监听工具条
    table.on('tool(list)', function (obj) {
        var data = obj.data;
        var openid = data.openid;
        var id = data.msg_id;
        if (obj.event === 'del') {
            layer.confirm('您确定要删除该信息吗？', function () {
                admin.req({
                    url: '/shengteng/wmsg/delMsg/'
                    , type: 'post'
                    , data: {openid:openid,id:id}
                    , success: function (res) {
                        layer.closeAll('dialog');
                        if (res.code == 0) {
                            layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                                obj.del();
                            });
                        }
                    }
                });
            });
        }
    });
    //图片回复
    var uploadInst = upload.render({
        elem: '#replyMsgImg'
        ,url: '/shengteng/wmsg/replyImgByMsg/openid/'+OPENID
        ,exts:'jpg|png|gif|bmp|jpeg'
        ,size: 2048
        ,field:'image'
        ,before: function(input){
            load = layer.load(1);
        }
        ,done: function(res){
            if(res.code==0){
                layer.close(load);
                layer.msg(res.msg)
                window.location.reload();
            }else{
                layer.close(load);
                layer.alert(res.msg);
            }
        }
        ,error: function(){
            layer.msg('请求出现异常');
        }
    });

    exports('replyMsg', {});
});
